package com.rchat.pocmini.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;

import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.view.ClearEditText;
import com.rchat.pocmini.common.mvp.BaseMvpActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ljd on 2017/7/26.
 *
 */

public class LoginMvpActivity extends BaseMvpActivity<LoginPresenterImpl,LoginModelImpl> implements LoginContract.View{

    /**
     * 账号编辑框
     */
    @BindView(R.id.et_account)
    ClearEditText mETAccount;
    /**
     * 密码编辑框
     */
    @BindView(R.id.et_password)
    ClearEditText mETPassword;

    /**
     * 记住密码的勾选框
     */
    @BindView(R.id.iv_remember_pwd_check)
    ImageView mIvRememberPwdCheck;

    /**
     * 自动登录的勾选框
     */
    @BindView(R.id.iv_auto_login_check)
    ImageView mIvAutoLoginCheck;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        /**
         * 解决：关于应该切到后台在点击桌面图标时，启动页是登录页面
         * 参数资料：http://blog.csdn.net/jianiuqi/article/details/54091181
         *           http://www.2cto.com/kf/201606/519729.html
         */
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
//            return;
        }
        super.onCreate(savedInstanceState);
    }



    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
    }

    @Override
    public int getLayoutId() {
        return R.layout.login_activity_layout;
    }

    @Override
    public void initView() {

        mETAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mETPassword.setText("");
            }
        });
    }

    @Override
    public void doAfterInitView() {

    }

    @OnClick({R.id.tv_register,
              R.id.tv_forget_pwd,
              R.id.iv_login,
              R.id.ll_auto_login,
//              R.id.tv_recharge,
              R.id.ll_remember_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_register:
                mPresenter.gotoRegisterActivity();
                break;
            case R.id.tv_forget_pwd:
                mPresenter.gotoForgetPwdActivity();
                break;
            case R.id.iv_login:
                mPresenter.login(mETAccount.getText().toString().trim(),
                                 mETPassword.getText().toString().trim());
                break;
            case R.id.ll_auto_login:
                mPresenter.setIvAutoLoginCheck();
                break;
            case R.id.ll_remember_pwd:
                mPresenter.setIvRememberPwdCheck();
                break;
//            case R.id.tv_recharge:
//                mPresenter.gotoRechargeActivity();
//                break;
        }
    }

    @Override
    public void setAccountEditText(String account) {
        mETAccount.setText(account);
    }

    @Override
    public void setPwdEditText(String pwd) {
        mETPassword.setText(pwd);
    }

    @Override
    public void setAutoLoginBackgroundImage(boolean autoLogin) {
        if (autoLogin) {
            mIvAutoLoginCheck.setImageResource(R.drawable.check_yes);
        } else {
            mIvAutoLoginCheck.setImageResource(R.drawable.check_no);
        }
    }

    @Override
    public void setRememberPwdBackgroundImage(boolean rememberPwd) {
        if (rememberPwd) {
            mIvRememberPwdCheck.setImageResource(R.drawable.check_yes);
        } else {
            mIvRememberPwdCheck.setImageResource(R.drawable.check_no);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 1:
                mPresenter.loginFromRegisterActivity(data);
        }
    }

}
